package map.android.baidu.carpool.data.impl;

import android.content.Context;
import com.baidu.lbs.net.http.model.ComNetData;
import map.android.baidu.carpool.WebConfig;
import map.android.baidu.carpool.model.CarpoolPosition;
import map.android.baidu.carpool.response.MainInfoResponse;
import map.android.baidu.carpool.setting.UserInfoSetting;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhangtongyu on 15/4/8.
 */
public class MainInfoData extends ComNetData<MainInfoResponse>{

    private CarpoolPosition startPlaceInfo;
    private CarpoolPosition arrivePlaceInfo;

    public MainInfoData(Context context) {
        super(context);
    }

    public void setStartPlaceInfo(CarpoolPosition startPlaceInfo){
        this.startPlaceInfo = startPlaceInfo;

    }

    public void setArrivePlaceInfo(CarpoolPosition arrivePlaceInfo) {
        this.arrivePlaceInfo = arrivePlaceInfo;
    }

    @Override
    protected String getUrl() {
        return WebConfig.getUrl(WebConfig.URL_ID_MAIN_INFO);
    }

    @Override
    protected Map<String, String> getParams() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("bduss", UserInfoSetting.getBduss());
        params.put("cityCode", UserInfoSetting.getCityId());
        if(startPlaceInfo != null){
            params.put("startPlaceInfo" , transLocalePlaceInfoToJsonObj(startPlaceInfo , 1).toString());
        }
        if(arrivePlaceInfo != null) {
            params.put("arrivePlaceInfo", transLocalePlaceInfoToJsonObj(arrivePlaceInfo, 2).toString());
        }
        return params;
    }

    /**
     *提供将LocalePlaceInfo类转换成Json的方法
     * @param localePlaceInfo 转换原料
     *        startOrArrive   出发地或目的地 value：1：start OR 2：arrive
     */
    private JSONObject transLocalePlaceInfoToJsonObj(CarpoolPosition localePlaceInfo , int startOrArrive){
        JSONObject jsonObject = new JSONObject();

        switch (startOrArrive) {
            case 1:
                try {
                    jsonObject.put("startPlaceGeo", String.valueOf(localePlaceInfo.x) + "," + String.valueOf(localePlaceInfo.y));
                    jsonObject.put("startPlace", localePlaceInfo.address);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case 2:
                try {
                    jsonObject.put("arrivePlaceGeo", String.valueOf(localePlaceInfo.x) + "," + String.valueOf(localePlaceInfo.y));
                    jsonObject.put("arrivePlace", localePlaceInfo.address);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
        }

        return jsonObject;
    }
}
